#import "MJRefresh.h"
#import "FTNewsDetailController.h"
#import "FTFootBallNews.h"
#import "ZQFTNewsCell.h"
#import "UIHelper.h"
#import <MJExtension/MJExtension.h>
#import "SDRefresh.h"
#import "ZQNewModes.h"
#import "ZQNewPage.h"
#import "AFNetworkingHelper.h"
#import "UIImageView+WebCache.h"
#define refreshNewsURL @"http://skyapp.mackentan.com/app/v6/article/list.do?t=1523170732000&did=UjGblPV7sR7a637ea771&since=&ver=172&it=422984&fit=422984&sign=33862098bc6f91287f73f1785e836007f067f4b5&appid=com.jediapp.football&idfa=E27CC654-F821-4B04-801C-40098E0D7018"
static NSString * const ID = @"cell";
@interface ZQNewPage ()<UITableViewDelegate,UITableViewDataSource>
{
        SDRefreshFooterView *_refreshFooter;
}
@property (nonatomic, assign) NSInteger count;
@property (nonatomic , strong) NSMutableArray  *topics;
@property (nonatomic , strong) ZQNewModes *model;
@property (nonatomic, assign) NSInteger npc;
@end
@implementation ZQNewPage
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title =  @"世界足球";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZQFTNewsCell class]) bundle:nil] forCellReuseIdentifier:ID];
    self.tableView.rowHeight = 310;
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    MJRefreshHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewNews)];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
#pragma mark - === loadNewNews ===
- (void)loadNewNews
{
    [self loadNewTopics];
    [self.tableView reloadData];
//    [self.tableView.mj_header endRefreshing];
}
- (NSMutableArray *)topics
{
    if (!_topics) {
        _topics = [NSMutableArray array];
    }
    return _topics;
}
- (void)loadNewTopics
{
    self.npc = 0;
    AFHTTPSessionManager *mgf = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"opc"] = @(20);
    parameters[@"type"] = @"国际足球";
    parameters[@"uid"] = @"5632";
    parameters[@"npc"] = @(self.npc);;
    NSLog(@"%@",[self backLoadURL]);
    [mgf POST:@"http://api.football.app887.com/api/Articles.action?keyword=&" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger count = (NSInteger)responseObject[@"root"][@"count"];
        if (self.count == count) {
            [self.tableView.mj_header endRefreshing];
            return ;
        }
        self.count = count;
        self.topics = [ZQNewModes mj_objectArrayWithKeyValuesArray:responseObject[@"root"][@"list"]];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络繁忙,稍后在试" inView:self.view];
    }];
}
- (void)loadMoreTopics
{
    self.npc++;
    AFHTTPSessionManager *mgf = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"opc"] = @(20);
    parameters[@"type"] = @"国际足球";
    parameters[@"uid"] = @"5632";
    parameters[@"npc"] = @(self.npc);;
    [mgf POST:@"http://api.football.app887.com/api/Articles.action?keyword=&" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.topics addObjectsFromArray:[ZQNewModes mj_objectArrayWithKeyValuesArray:responseObject[@"root"][@"list"]]];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"网络繁忙,稍后在试" inView:self.view];
    }];
}
- (NSString *)backLoadURL
{
    return @"";
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.topics.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZQFTNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.footballNews = self.topics[indexPath.row];
    return cell;
}
#pragma mark - tableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FTFootBallNews *news = self.topics[indexPath.row];
    FTNewsDetailController *detailVC = [[FTNewsDetailController alloc] init];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.ID = news.ID;
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
